﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RhythmComet.Physics;
using RhythmComet.Utils;

namespace RhythmComet.Particles
{
    public class ParticleSystemManager
    {
        RhythmCometGame game;
        int lastIndex;
        ParticleSystem[] psList;

        public ParticleSystemManager(Game game, int total)
        {
            this.game = (RhythmCometGame) game;
            psList = new ParticleSystem[total];
            lastIndex = -1;
        }

        public void Add(ParticleSystem ps)
        {
            if (lastIndex < psList.Length - 1)
            {
                psList[++lastIndex] = ps;
            }
        }

        public void Update(GameTime gameTime)
        {
            for (int i = lastIndex; i >= 0; i--)
            {
                if (psList[i].Dead)
                    psList[i] = psList[lastIndex--];
                else
                    psList[i].Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            for (int i = lastIndex; i >= 0; i--)
            {
                psList[i].Draw(gameTime);
            }
        }
    }
}
